#ifndef H_RCGD_CONTROL_H
#define H_RCGD_CONTROL_H

/*=================================================================*/
/*                            define                               */
/*=================================================================*/
#define     Survey_Search       0
#define     Survey_InitializePLL      2
#define		Survey_QuickStart	4

#define		Survey_UpdateDetailTable	5


#define CTRL_PRE_QUICKSTART	0
#define CTRL_QUICKSTART	1
#define CTRL_NORMAL		2

#define CTRL_OCXO_HEATUP_NOT_READY		1
#define CTRL_OCXO_HEATUP_READY			2

#define FREE_RUN			'F'
#define REFERENCE_GPS_RUN	'G'
#define REFERENCE_8K_RUN	'E'

#define Kinterval_STEP_1 5		//120625	sewon
#define Kinterval_STEP_2 15
#define Kinterval_STEP_3 30
#define Kinterval_STEP_4 60

#define Kinterval_STEP_Count_1 100		//120625	sewon
#define Kinterval_STEP_Count_2 129
#define Kinterval_STEP_Count_3 158

//#define Kinterval_STEP_Count_1 150		//#120814#	sewon
//#define Kinterval_STEP_Count_2 250
//#define Kinterval_STEP_Count_3 350
/*=================================================================*/
/*                           CONTROL.C                             */
/*=================================================================*/
#define WARMUP_AVERAGE_INTERVAL		10

#define WARMUP_READY_RANGE			0.5
//#define WARMUP_READY_RANGE			1.5  //kang:temp

#define WARMUP_READY_TIME			30

typedef struct CONTROL_S{

    /* int	Step_Counter; */

    char    SURVEYSTEP;
    char	SURVEY_MODE; /* 3D-fix or 2D-fix */

    /* Binary search */
    char    WarmupStep;

    unsigned short  HighestDAC,LowestDAC;


    double   RealDAC,PrevDAC,NominalDAC;

    double	 ControlDAC;

    double   PrevTimeOffset,TimeOffset,AvgTimeOffset;
    double   PrevSlope,Slope;

    double	 Frequency;

    int  CurrentDAC;


    short   HigherFraction,LowerFraction;

    int     IntervalCounter,HoldoverDuration;

    int     CV_Control_Counter;


    char    SYNC;
    char    TRACE_SYNC;
	char	EVEN_SYNC;

    char    First;
    short	PWMhit;

   	int		WaitToControlCounter;
   	int		WaitToALMANACCounter;

	int		StepCounter;

	char 	CTRLMODE;

	char	Run_Mode;

	int		warmup_looks_good;
	double	warmup_avgmin;
	double	warmup_avgmax;
	
	int		warmup_counter;

	int		warmup_insertcounter;
	float	warmup_averagetable[WARMUP_AVERAGE_INTERVAL];
	float	warmup_average;
	
	int		warmup_avgavgcounter;
	double	warmup_avgavgtable[WARMUP_AVERAGE_INTERVAL];
	
} CONTROL_;






#define GPS_CUTOFF 		300    /* 300 nsec */



extern char	LOCK_REFERENCE;

#define MODULE_WARMUP_TIME_30MIN	(30*60)
#define MODULE_WARMUP_TIME_10MIN	(10*60)
#define WARMUP_TIME_BOUNDARY		15.0

extern char Update_ModuleWarmupTime_flag;
extern int Module_Warmup_Time;

extern void Update_ModuleWarmupTime(float temper);
extern void RbSRO_Control();

extern int         bDacUpdated;
extern int esync_debug;
extern int control_sync_type;
extern int iCntEvenSyncDly;
extern char sync_retry;

extern unsigned int freq_stable_cnt;



#define MODE_HLD1	1
#define MODE_HLD2	2
#define MODE_HLD3	3
#define MODE_HLD4	4
#define MODE_LOCK	0
#define MODE_RCV1	6
#define MODE_RCV2	7


#define aveLimit		50
#define freqGoodLimit	5E-10
/*#define freqFailLimit	2E-9*/
/*#define freqFailLimit	3.5E-9*/
#define freqFailLimit	5.0E-9
//#define DACGain			1.24E-11;
#define DACGain			KGainF;
#define ffRestCriteria	5

#define FF_FIRST 1
#define FF_UNLOCK 2
#define FF_UNSTABLE 0
#define FF_OTHERS 3
#define FF_STABLE 4



typedef struct
{
	double sumDAC;
	double sumTEMP;

	double centerDAC;
	double centerTEMP;

	int cntGood;
}_aveRO;

typedef struct _FreqFilter	FreqFilter;
struct _FreqFilter
{
	int	turn;
	int once;
	int ReceiverGood;
	int bCalAvail;
	double Freq;
	double diffFreq;


	int Index;
	int IndexAve;

	int cntData;
	int freqFailCnt;

	double dRO[aveLimit];
	double dROsum;
	double AvedRO[aveLimit];

	_aveRO	Ref[2];

};

extern FreqFilter ffD;

extern int bFirstFreqFilter;
extern int bResetReceiver;
extern int ffPush( int Lock, double RO, double DAC,double TEMP);
extern int sync_by_ddac;
extern char control_mode;
extern int warmup_stable_retry;
extern char turn;
extern double pref_centerdac;
extern int prev_running_time, prev_power_time;
extern int		QuickStartCounter;

extern void SetEbDummy();
void ChangeControlInterval(int interval);
void Init_QuickStart();
void Clear1PPSwithGPS();	//sewon
#endif
